<?php
/*
 * Copyright (c) 2011 Todd Brady
 *
 * PS3Theatre is freely distributable under the terms of the MIT license.
 * For details, see the MIT-Licence web site: http://www.opensource.org/licenses/mit-license.php 
 */
 
 class StringUtil
    {
		private static $removeChars = array("'",".","/",",","[","]","{","}","(",")","|","-","_",":",";");
		
		//validate tv show filename
		public static function validateTvShowFilename($filename){
			return (preg_match("/[sS]{1}[0-9]{2}[eE][0-9]{2}/", $filename, $matches, PREG_OFFSET_CAPTURE) == 1);
		}

		//validate movie filename
		static public function validateMovieFilename($filename){
			//not yet implemented
		}
		
		//clean title
		static public function createCleanTitle($filename){
			$cleanTitle = '';
			
			$cleanTitle = self::getJustFilename($filename);

			//look for season ep string in title and remove it
			$cleanTitle = preg_replace("/[sS]{1}[0-9]{2}[eE][0-9]{2}/",'',$cleanTitle); 
			
			//look for year in string and remove it
			$cleanTitle = preg_replace("/[0-9]{4}/",'',$cleanTitle); 
			
			//remove special chars
			$cleanTitle = self::stripSpecialChars($cleanTitle);
			
			return strtolower($cleanTitle);
		}
		
		//get extension from filename
		static public function getExtension($filename){
			$ext = '';
			
			$extStartPos = strrpos($filename, '.');
			
			if($extStartPos === false){
				$ext = false;
			}else{
				$ext = substr($filename, $extStartPos+1);
			}
			
			return $ext;
		}
		
		//strip extension from filename, if there is no extension just return the filename
		static public function getJustFilename($filename){
			$justFileName = '';
			
			$extStartPos = strrpos($filename, '.');

			if($extStartPos === false){
				$justFileName = $filename;
			}else{
				$justFileName = substr($filename, 0, $extStartPos);
			}
			
			return $justFileName;
		}
		
		//get season number
		static public function getTvShowSeason($filename){
			preg_match("/[sS]{1}[0-9]{2}[eE][0-9]{2}/", $filename, $matches, PREG_OFFSET_CAPTURE);
			return  (int)substr($matches[0][0],1,2);
		}
		
		//get ep number
		static public function getTvShowEpisode($filename){
			preg_match("/[sS]{1}[0-9]{2}[eE][0-9]{2}/", $filename, $matches, PREG_OFFSET_CAPTURE);
			return (int)substr($matches[0][0],4,2);
		}
		
		//get year
		static public function getYear($filename){
			if(preg_match("/[0-9]{4}/", $filename, $matches, PREG_OFFSET_CAPTURE)){
				return (int)$matches[0][0];
			}else{
				return false;
			}
		}
		
		//returns true if haystack starts with needle
		static public function startsWith($haystack, $needle)
		{
			$length = strlen($needle);
			return (substr($haystack, 0, $length) === $needle);
		}

		//returns true if haystack ends with needle
		static public function endsWith($haystack, $needle)
		{
			$length = strlen($needle);
			$start  = $length * -1; //negative
			return (substr($haystack, $start) === $needle);
		}
		
		//
		static private function stripSpecialChars($filename){
			// remove special chars
			$cleanFileName = str_replace(self::$removeChars, '', $filename);

			//replace multiple spaces with 1
			$cleanFileName = preg_replace('#\s{2,}#',' ',$cleanFileName); 

			return trim($cleanFileName);
		}
	}
?>